#!/usr/bin/python



import xpcom
from xpcom import *


print "xul_test loading"

'''
def test():
	print "TEEEEST!!!"


print "window = " + str(window)
print "addEvt: " + str(window.addEventListener("load", test, True))


print "xul_test \"loaded\""
exit(0)
'''


def function():
	print "i'm in fct method"

def function2():
	print "i'm in fct2 method"




############################################################

"""
var myExt_urlBarListener = {
		QueryInterface: function(aIID)
		{
				if (aIID.equals(components.interfaces.nsIWebProgressListener) ||
				aIID.equals(components.interfaces.nsISupportsWeakReference) ||
				aIID.equals(components.interfaces.nsISupports))
				return this;
				throw components.results.NS_NOINTERFACE;
		},

		onLocationChange: function(aProgress, aRequest, aURI)
		{
				myExtension.processNewURL(aURI);
		}
};
"""

class Ext_urlBarListener:
	
	def QueryInterface(self, aIID):
		
		if (aIID == components.interfaces.nsIWebProgressListener) or (aIID == components.interfaces.nsISupportsWeakReference) or (aIID == components.interfaces.nsISupports):
			return self
		
	def onLocationChange (aProgress, aRequest, aURI):
		
		myExtension.processNewURL(aURI)

############################################################

def fct():
	
	print "WOOOOOOORKIIING!!!"

############################################################

class Extension:
	
	oldURL = None # static attribute
	
	def init(self, event):
		
		"""
		elt1 = window.QueryInterface(components.interfaces.nsIInterfaceRequestor).getInterface(components.interfaces.nsIWebNavigation)
		print "elt1 = " + str(elt1)
		
		elt2 = elt1.QueryInterface(components.interfaces.nsIDocShellTreeItem).rootTreeItem
		print "elt2 = " + str(elt2)
		
		elt3 = elt2.QueryInterface(components.interfaces.nsIInterfaceRequestor).getInterface(components.interfaces.nsIDOMWindow)
		
		print "elt3 = " + str(elt3)
		"""
		
		print document.getElementById("content")
		
		browser = document.getElementById("browser")
		print "browser(hbox) = " + str(browser)
		
		appcontents = browser.getElementsByAttribute("id", "appcontent")
		print "appcontents(vboxes) = " + str(appcontents)
		appcontent = appcontents.item(0)
		print "appcontent(vbox) = " + str(appcontent)
		
		contents = appcontent.getElementsByAttribute("id", "content")
		print "contents(tabbrowsers) = " + str(contents)
		content = contents.item(0)
		print "content(tabbrowser) = " + str(content)
		gBrowser = document.getElementById("content");
		content.addTab("http://www.google.fr")
		
		this = window
		for attr in "content document parent top scrollbars name textZoom scrollX scrollY".split():
			try:
					val = getattr(this, attr)
			except (xpcom.Exception, AttributeError), exc:
					print "FAILED to get attribute %s: %s" % (attr, exc)
			else:
					print attr, "is", val
		print "Scrollbar visible =", this.scrollbars.visible
		
		#promptService = xpcom.components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(xpcom.components.interfaces.nsIPromptService)
		#promptService.alert(this.window, "title", "Hello from Python")
		
		#browser = window.top.document.getElementById("content")
		#tab = browser.addTab("http://www.google.com")
		
		kWindowMediatorContractID = "@mozilla.org/appshell/window-mediator;1"
		kWindowMediatorIID = components.interfaces.nsIWindowMediator
		kWindowMediator = components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID)
		browserWindow = kWindowMediator.getMostRecentWindow("navigator:browser")
		browser = browserWindow.getBrowser()
		tab = browser.addTab("http://www.google.com")
		
		content = document.getElementById("content")
		print "content = " + str(content)
		
		content.addProgressListener(myExt_urlBarListener, components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT)
		
		"""
		print "self = "+str(self)
		print "event = "+str(event)
		browser = document.getElementById("browser")
		print "browser = "+str(browser)
		browser.addProgressListener(myExt_urlBarListener, components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT)
		"""
		
	def uninit(self, event):
		
		browser.removeProgressListener(myExt_urlBarListener)
		
	def processNewURL(aURI):
		
		if not (aURI.spec == self.oldURL):
			# now we know the url is new
			document.getElementById("rateLabel").setAttribute("value",aURI.spec)
			this.oldURL = aURI.spec

"""
var myExtension = {
	oldURL: null,

	init: function() {
			// Listen for webpage loads
			gBrowser.addProgressListener(myExt_urlBarListener,
			components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
	},

	uninit: function() {
		gBrowser.removeProgressListener(myExt_urlBarListener);
	},

	processNewURL: function(aURI) {
			if (aURI.spec == this.oldURL)
				return;
				
			// now we know the url is new...
			document.getElementById("rateLabel").setAttribute("value",aURI.spec)
			//alert(aURI.spec);
			this.oldURL = aURI.spec;
	}
};

window.addEventListener("load", function() {myExtension.init()}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);
"""

myExtension = Extension()
myExt_urlBarListener = Ext_urlBarListener()

window.addEventListener('load', myExtension.init, False)
window.addEventListener('unload', myExtension.uninit, False)

print "xul_test loaded"





